home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH19 / MULTIDOS.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-21  |  5.3 KB  |  238 lines

  1. ; MULTIDOS.ASM
  2. ;
  3. ; This program demonstrates how to use semaphores to protect DOS calls.
  4.  
  5.         .xlist
  6.         include     stdlib.a
  7.         includelib    stdlib.lib
  8.         .list
  9.  
  10.  
  11. dseg        segment    para public 'data'
  12.  
  13. DOSsmaph    semaphore    {}
  14.  
  15. ; Macros to wait and release the DOS semaphore:
  16.  
  17. DOSWait        macro
  18.         push    es
  19.         push    di
  20.         lesi    DOSsmaph
  21.         WaitSemaph
  22.         pop    di
  23.         pop    es
  24.         endm
  25.  
  26. DOSRls        macro
  27.         push    es
  28.         push    di
  29.         lesi    DOSsmaph
  30.         RlsSemaph
  31.         pop    di
  32.         pop    es
  33.         endm
  34.  
  35.  
  36.  
  37.  
  38. ; PCB for our background process:
  39.  
  40. BkgndPCB    pcb    {0,offset EndStk2, seg EndStk2}
  41.  
  42. ; Data the foreground and background processes print:
  43.  
  44. StrPtrs1    dword    str1_a, str1_b, str1_c, str1_d, str1_e, str1_f
  45.         dword    str1_g, str1_h, str1_i, str1_j, str1_k, str1_l
  46.         dword    0
  47.  
  48. str1_a        byte    "Foreground: string 'a'",cr,lf,0
  49. str1_b        byte    "Foreground: string 'b'",cr,lf,0
  50. str1_c        byte    "Foreground: string 'c'",cr,lf,0
  51. str1_d        byte    "Foreground: string 'd'",cr,lf,0
  52. str1_e        byte    "Foreground: string 'e'",cr,lf,0
  53. str1_f        byte    "Foreground: string 'f'",cr,lf,0
  54. str1_g        byte    "Foreground: string 'g'",cr,lf,0
  55. str1_h        byte    "Foreground: string 'h'",cr,lf,0
  56. str1_i        byte    "Foreground: string 'i'",cr,lf,0
  57. str1_j        byte    "Foreground: string 'j'",cr,lf,0
  58. str1_k        byte    "Foreground: string 'k'",cr,lf,0
  59. str1_l        byte    "Foreground: string 'l'",cr,lf,0
  60.  
  61. StrPtrs2    dword    str2_a, str2_b, str2_c, str2_d, str2_e, str2_f
  62.         dword    str2_g, str2_h, str2_i
  63.         dword    0
  64.  
  65. str2_a        byte    "Background: string 'a'",cr,lf,0
  66. str2_b        byte    "Background: string 'b'",cr,lf,0
  67. str2_c        byte    "Background: string 'c'",cr,lf,0
  68. str2_d        byte    "Background: string 'd'",cr,lf,0
  69. str2_e        byte    "Background: string 'e'",cr,lf,0
  70. str2_f        byte    "Background: string 'f'",cr,lf,0
  71. str2_g        byte    "Background: string 'g'",cr,lf,0
  72. str2_h        byte    "Background: string 'h'",cr,lf,0
  73. str2_i        byte    "Background: string 'i'",cr,lf,0
  74.  
  75. dseg        ends
  76.  
  77. cseg        segment    para public 'code'
  78.         assume    cs:cseg, ds:dseg
  79.  
  80. ; A replacement critical error handler.  This routine calls prcsquit
  81. ; if the user decides to abort the program.
  82.  
  83.  
  84. CritErrMsg    byte    cr,lf
  85.         byte    "DOS Critical Error!",cr,lf
  86.         byte    "A)bort, R)etry, I)gnore, F)ail? $"
  87.  
  88. MyInt24        proc    far
  89.         push    dx
  90.         push    ds
  91.         push    ax
  92.  
  93.         push    cs
  94.         pop    ds
  95. Int24Lp:    lea    dx, CritErrMsg
  96.         mov    ah, 9            ;DOS print string call.
  97.         int    21h
  98.  
  99.         mov    ah, 1            ;DOS read character call.
  100.         int    21h
  101.         and    al, 5Fh            ;Convert l.c. -> u.c.
  102.  
  103.         cmp    al, 'I'            ;Ignore?
  104.         jne    NotIgnore
  105.         pop    ax
  106.         mov    al, 0
  107.         jmp    Quit24
  108.  
  109. NotIgnore:    cmp    al, 'r'            ;Retry?
  110.         jne    NotRetry
  111.         pop    ax
  112.         mov    al, 1
  113.         jmp    Quit24
  114.  
  115. NotRetry:    cmp    al, 'A'            ;Abort?
  116.         jne    NotAbort
  117.         prcsquit            ;If quitting, fix INT 8.
  118.         pop    ax
  119.         mov    al, 2
  120.         jmp    Quit24
  121.  
  122. NotAbort:    cmp    al, 'F'
  123.         jne    BadChar
  124.         pop    ax
  125.         mov    al, 3
  126. Quit24:        pop    ds
  127.         pop    dx
  128.         iret
  129.  
  130. BadChar:    mov    ah, 2
  131.         mov    dl, 7            ;Bell character
  132.         jmp    Int24Lp
  133. MyInt24        endp
  134.  
  135.  
  136.  
  137. ; We will simply disable INT 23h (the break exception).
  138.  
  139. MyInt23        proc    far
  140.         iret
  141. MyInt23         endp
  142.  
  143.  
  144.  
  145. ; This background process calls DOS to print several strings to the
  146. ; screen.  In the meantime, the foreground process is also printing
  147. ; strings to the screen.  To prevent reentry, or at least a jumble of
  148. ; characters on the screen, this code uses semaphores to protect the
  149. ; DOS calls.  Therefore, each process will print one complete line
  150. ; then release the semaphore.  If the other process is waiting it will
  151. ; print its line.
  152.  
  153. BackGround    proc
  154.         mov    ax, dseg
  155.         mov    ds, ax
  156.         lea    bx, StrPtrs2        ;Array of str ptrs.
  157. PrintLoop:    cmp    word ptr [bx+2], 0    ;At end of pointers?
  158.         je    BkGndDone
  159.         les    di, [bx]        ;Get string to print.
  160.         DOSWait
  161.         puts                ;Calls DOS to print string.
  162.         DOSRls
  163.         add    bx, 4            ;Point at next str ptr.
  164.         jmp    PrintLoop
  165.  
  166. BkGndDone:    die
  167. BackGround    endp
  168.  
  169.  
  170. Main        proc
  171.         mov    ax, dseg
  172.         mov    ds, ax
  173.         mov    es, ax
  174.         meminit
  175.  
  176.  
  177. ; Initialize the INT 23h and INT 24h exception handler vectors.
  178.  
  179.         mov    ax, 0
  180.         mov    es, ax
  181.         mov    word ptr es:[24h*4], offset MyInt24
  182.         mov    es:[24h*4 + 2], cs
  183.         mov    word ptr es:[23h*4], offset MyInt23
  184.         mov    es:[23h*4 + 2], cs
  185.  
  186.         prcsinit        ;Start multitasking system.
  187.  
  188.         lesi    BkgndPCB    ;Fire up a new process
  189.         fork
  190.         test    ax, ax        ;Parent's return?
  191.         je    ParentPrcs
  192.         jmp    BackGround    ;Go do backgroun stuff.
  193.  
  194. ; The parent process will print a bunch of strings at the same time
  195. ; the background process is doing this.  We'll use the DOS semaphore
  196. ; to protect the call to DOS that PUTS makes.
  197.  
  198. ParentPrcs:     DOSWait                ;Force the other process
  199.         mov    cx, 0            ; to wind up waiting in
  200. DlyLp0:        loop    DlyLp0            ; the semaphore queue by
  201. DlyLp1:        loop    DlyLp1            ; delay for at least one
  202. DlyLp2:        loop    DlyLp2            ; clock tick.
  203.         DOSRls
  204.  
  205.         lea    bx, StrPtrs1        ;Array of str ptrs.
  206. PrintLoop:    cmp    word ptr [bx+2], 0    ;At end of pointers?
  207.         je    ForeGndDone
  208.         les    di, [bx]        ;Get string to print.
  209.         DOSWait
  210.         puts                ;Calls DOS to print string.
  211.         DOSRls
  212.         add    bx, 4            ;Point at next str ptr.
  213.         jmp    PrintLoop
  214.  
  215. ForeGndDone:    prcsquit
  216.  
  217. Quit:        ExitPgm                ;DOS macro to quit program.
  218. Main        endp
  219.  
  220. cseg            ends
  221.  
  222. sseg        segment    para stack 'stack'
  223.  
  224. ; Here is the stack for the background process we start
  225.  
  226. stk2        byte    1024 dup (?)
  227. EndStk2        word    ?
  228.  
  229. ;Here's the stack for the main program/foreground process.
  230.  
  231. stk        byte    1024 dup (?)
  232. sseg        ends
  233.  
  234. zzzzzzseg    segment    para public 'zzzzzz'
  235. LastBytes    db    16 dup (?)
  236. zzzzzzseg    ends
  237.         end    Main
  238.